﻿// DrillViewDlg.cpp: 实现文件
//

//#include "pch.h"
//#include "QuantyView3D.h"
#include"../../../StdAfx.h"
#include "DrillViewDlg.h"
#include "afxdialogex.h"
#include"resource2.h"
#include"../../modeling3D/DrillVisual.h"
#include<set>
using std::set;


// DrillViewDlg 对话框

IMPLEMENT_DYNAMIC(DrillViewDlg, CDialogEx)

DrillViewDlg::DrillViewDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DRILLVIEW, pParent)
{
	//变量绑定
	kantanqu = new CComboBox();
	drill_list = new CListBox();
	oreName = new CComboBox;
}

DrillViewDlg::~DrillViewDlg()
{
}

void DrillViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_KANTANQU_NO, *kantanqu);
	DDX_Control(pDX, IDC_LIST_DRILL_NO, *drill_list);
	DDX_Control(pDX, IDC_COMBO_ORENAME, *oreName);
}

//INT_PTR DrillViewDlg::DoModal()
//{
//	INT_PTR re = CDialogEx::DoModal();
//
//	//查询勘探区
//
//

//	return re;
//}


BEGIN_MESSAGE_MAP(DrillViewDlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_KANTANQU_NO, &DrillViewDlg::OnCbnSelchangeKantanquNo)
	ON_BN_CLICKED(IDC_BUTTON_FLOOR_VIEW, &DrillViewDlg::OnBnClickedButtonFloorView)
	ON_BN_CLICKED(IDC_BUTTON_ORE_VIEW, &DrillViewDlg::OnBnClickedButtonOreView)
END_MESSAGE_MAP()



void DrillViewDlg::setInitData(User_ST user)
{
	this->user = user;
}

void DrillViewDlg::setMap(CGV3dMap* map)
{
	pMap = map;
}


// DrillViewDlg 消息处理程序


void DrillViewDlg::OnCbnSelchangeKantanquNo()
{
	// TODO: 在此添加控件通知处理程序代码
	//选择的勘探区发生了改变
	//钻孔显示发生变化
	drill_list->ResetContent();
	CString kantan_no;
	int index = kantanqu->GetCurSel();
	if (index == -1)
		return;
	kantanqu->GetLBText(index, kantan_no);

	if (drills.size() == 0)
		return;

	for (unsigned int i = 0; i < drills.size(); i++)
	{
		if (drills[i].kantanqu_no.c_str() == kantan_no)
		{
			drill_list->AddString(drills[i].drill_no.c_str());
		}
	}

	//矿体显示发生变化
	oreName->Clear();
	//进行矿种的显示
	OrebodyTypeDAO otd;
	vector<OreGrade_ST> ores = otd.selectDrill(kantan_no.GetString(), drills[1].drill_no);
	set<string> ore;
	for (unsigned int i = 0; i < ores.size(); i++)
	{
		ore.insert(ores[i].name);
		
	}
	for (auto it = ore.begin(); it != ore.end(); it++)
	{
		oreName->AddString(it->c_str());
	}
}




void DrillViewDlg::OnBnClickedButtonFloorView()
{
	// TODO: 在此添加控件通知处理程序代码
	//地层可视化
	CString kantan_no;
	int index = kantanqu->GetCurSel();
	if (index == -1)
		return;
	kantanqu->GetLBText(index, kantan_no);
	//获取对应的钻孔信息
	int nums=drill_list->GetSelCount();
	int* pnSelIndex = new int[nums];
	drill_list->GetSelItems(nums, pnSelIndex);
	vector<string> drills_no;
	for (int i = 0; i < nums; i++)
	{
		CString na;
		drill_list->GetText(pnSelIndex[i], na);
		drills_no.push_back(na.GetString());
	}
	delete[] pnSelIndex;
	//绘制钻孔
	CGV3dLayer* pLay = new CGV3dLayer;
	CString layerName;
	GetDlgItemText(IDC_EDIT_LAYER_VIEW, layerName); 
	pLay->SetName(layerName+"岩芯");
	CGV3dLayer* pSur = new CGV3dLayer;
	pSur->SetName(layerName+"圆柱");
	pMap->AddLayer(pLay);
	pMap->AddLayer(pSur);

	DrillCexieDAO dcd;//测斜
	DrillFloorDAO dfd;//地层

	for (unsigned int i = 0; i < drills.size(); i++)
	{
		if (find(drills_no.begin(), drills_no.end(), drills[i].drill_no) != drills_no.end())
		{
			//绘制钻孔
			DrillVisual dv;
			XYZ p = { drills[i].x,drills[i].y,drills[i].z };
			dv.setGeometry(dcd.selectAll(kantan_no.GetString(), drills[i].drill_no), p, drills[i].deepth);
			vector<CGV3dPolyline*> lines = dv.buildFloorModel(dfd.selectAll(kantan_no.GetString(), drills[i].drill_no));
			//对每一条线获取圆柱
			for (unsigned int j = 0; j < lines.size(); j++)
			{
				pLay->AddObject(lines[j]);
				pSur->AddObject(dv.getSurface(lines[j]));
			}
		}
	}
	EndDialog(0);
}




void DrillViewDlg::OnBnClickedButtonOreView()
{
	// TODO: 在此添加控件通知处理程序代码
	//绘制矿体
	CString kantan_no;
	int index = kantanqu->GetCurSel();
	if (index == -1)
		return;
	kantanqu->GetLBText(index, kantan_no);
	//获取对应的钻孔信息
	//获取对应的钻孔信息
	int nums = drill_list->GetSelCount();
	int* pnSelIndex = new int[nums];
	drill_list->GetSelItems(nums, pnSelIndex);
	vector<string> drills_no;
	for (int i = 0; i < nums; i++)
	{
		CString na;
		drill_list->GetText(pnSelIndex[i], na);
		drills_no.push_back(na.GetString());
	}
	delete[] pnSelIndex;
	//绘制钻孔
	MineLayer* pLay = new MineLayer;
	CString layerName;
	GetDlgItemText(IDC_EDIT_LAYER_VIEW, layerName);
	int ore_index = oreName->GetCurSel();
	if (ore_index == -1)
		return;
	CString ore_selname;
	oreName->GetLBText(ore_index, ore_selname);
	pLay->SetName(layerName + "岩芯");
	CGV3dLayer* pSur = new CGV3dLayer;
	pSur->SetName(layerName + "圆柱");
	pMap->AddLayer(pLay);
	pMap->AddLayer(pSur);


	CString cut;
	GetDlgItemText(IDC_EDIT_CUTOFFGRADE, cut);
	double cutoff = atof(cut);


	DrillCexieDAO dcd;//测斜
	OrebodyTypeDAO otd;//矿体信息

	for (unsigned int i = 0; i < drills.size(); i++)
	{
		if (find(drills_no.begin(), drills_no.end(), drills[i].drill_no) != drills_no.end())
		{
			//绘制钻孔
			DrillVisual dv;
			XYZ p = { drills[i].x,drills[i].y,drills[i].z };
			dv.setGeometry(dcd.selectAll(kantan_no.GetString(), drills[i].drill_no), p, drills[i].deepth);
			vector<MineLine*> lines = dv.buildOreModel(otd.selectOreDrill(kantan_no.GetString(), drills[i].drill_no, ore_selname.GetString()), cutoff);
			//对每一条线获取圆柱
			for (unsigned int j = 0; j < lines.size(); j++)
			{
				pLay->AddObject(lines[j]);
				pSur->AddObject(dv.getSurface(lines[j]));
			}
		}
	}
	EndDialog(0);
}


BOOL DrillViewDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化

	UserDrillDao udd;
	drills = udd.selectExplor(user);
	//筛选出勘探区
	set<string> kantan_no;
	for (unsigned int i = 0; i < drills.size(); i++)
	{
		//插入数据
		kantan_no.insert(drills[i].kantanqu_no);
	}
	//将勘探区写入ui中
	kantanqu->Clear();//清空当前内容
	for (auto it = kantan_no.begin(); it != kantan_no.end(); it++)
	{
		kantanqu->AddString(it->c_str());
	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}
